<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" :model="queryParams" class="demo-form-inline">
        <el-form-item label="商品名称">
          <el-input v-model="queryParams.goodsName" placeholder="请输入商品名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <el-table
      v-loading="loading"
      :data="commentList"
      border
      style="width: 100%"
    >
      <el-table-column prop="commentId" label="ID" width="80" align="center" />
      <el-table-column prop="userId" label="用户ID" width="80" align="center" />
      <el-table-column prop="goodsId" label="商品ID" width="80" align="center" />
      <el-table-column prop="orderId" label="订单ID" width="80" align="center" />
      <el-table-column prop="rating" label="评分" width="80" align="center">
        <template #default="scope">
          <el-rate v-model="scope.row.rating" disabled />
        </template>
      </el-table-column>
      <el-table-column prop="comment" label="评论内容" min-width="200" show-overflow-tooltip />
      <el-table-column prop="commentTime" label="评论时间" min-width="160" />
      <el-table-column label="操作" width="150" align="center">
        <template #default="scope">
          <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getCommentPage, deleteComment } from '@/api/comment'

// 评论列表
const commentList = ref([])
// 总数据量
const total = ref(0)
// 加载状态
const loading = ref(false)

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  goodsName: ''
})

// 初始化数据
onMounted(async () => {
  await fetchData()
})

// 获取评论列表数据
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getCommentPage(queryParams)
    commentList.value = res.data.list || []
    total.value = res.data.total || 0
    console.log('获取到评论数据:', commentList.value)
  } catch (error) {
    console.error('获取评论列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 处理查询
const handleQuery = () => {
  queryParams.pageNum = 1
  fetchData()
}

// 重置查询
const resetQuery = () => {
  queryParams.goodsName = ''
  handleQuery()
}

// 处理页码变化
const handleCurrentChange = (val) => {
  queryParams.pageNum = val
  fetchData()
}

// 处理每页条数变化
const handleSizeChange = (val) => {
  queryParams.pageSize = val
  queryParams.pageNum = 1
  fetchData()
}

// 处理删除评论
const handleDelete = (row) => {
  ElMessageBox.confirm(
    `确认删除该评论吗？`,
    '警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(async () => {
    try {
      await deleteComment(row.commentId)
      ElMessage({
        type: 'success',
        message: '删除成功!'
      })
      await fetchData()
    } catch (error) {
      console.error('删除评论失败:', error)
    }
  }).catch(() => {
    // 取消删除
  })
}
</script> 